package com.game.modular.game.playerMatch.service.base;


import com.game.modular.game.playerMatch.domain.PvpCity;
import com.game.modular.game.playerMatch.domain.PvpMatch;
import com.game.modular.gameCore.map.domain.CoreCity;
import com.game.modular.gameCore.map.service.impl.CoreCityServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 城池信息基础服务
 */
@Slf4j
@Service
public class PvpCityBaseServiceImpl{
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private HashOperations<Object, Object, PvpCity> hashOperations;
    @Resource
    private CoreCityServiceImpl coreCityServiceImpl;

    private static final String redisKey = "pvpMatch:%d:city";

    /**
     * 更新
     */
    public PvpCity update(PvpCity pvpCity) {
        savaCache(pvpCity);
        return pvpCity;
    }

    /**
     * 初始化城池信息
     */
    public List<PvpCity> init(PvpMatch pvpMatch) {
        Long pvpMatchId = pvpMatch.getId();
        List<CoreCity> list = coreCityServiceImpl.listCache();
        List<PvpCity> pvpCities = new ArrayList<>();
        list.forEach(coreCity -> {
            PvpCity build = PvpCity.builder()
                    .matchId(pvpMatchId)
                    .lastOccupyTime(new Date())

                    .coreCityId(coreCity.getId())
                    .type(coreCity.getType())
                    .status(0)
                    // 初始化生产力
                    .grainRate(coreCity.getGrainRate())
                    .copperRate(coreCity.getCopperRate())
                    .readyArmyRate(coreCity.getReadyArmyRate())
                    //
                    .build();
            savaCache(build);
            pvpCities.add(build);
        });
        return pvpCities;
    }

    public void savaCache(PvpCity pvpCity) {
        String key1 = String.format(redisKey, pvpCity.getMatchId());
        String key2 = String.valueOf(pvpCity.getCoreCityId());
        hashOperations.put(key1, key2, pvpCity);
        redisTemplate.expire(key1, 300, TimeUnit.MINUTES);
    }

    public PvpCity getCache(@NotNull(message = "对局id不能为空") Long matchId, Object coreCityId) {
        String key1 = String.format(redisKey, matchId);
        String key2 = String.valueOf(coreCityId);
        return hashOperations.get(key1, key2);
    }

    public List<PvpCity> listCache(Long matchId) {
        String key1 = String.format(redisKey, matchId);
        Collection<PvpCity> values = hashOperations.entries(key1).values();
        return new ArrayList<>(values);
    }

    public void removeCache(Long matchId) {
        String key1 = String.format(redisKey, matchId);
        redisTemplate.delete(key1);
    }
}
